import requests
from bs4 import BeautifulSoup
import logging
import xlwt
import os
import threading

# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding = 'utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('My Worksheet')
worksheet.write(0,0, label = '书名')
worksheet.write(0,1, label = '路径')

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')  # logging.basicConfig函数对日志的输出格式及方式做相关配置

def getHtml():
    num = 0
    list = []
    for j in range(2,250,25):
        urlstr = "https://www.mzitu.com/page/{}/".format(j)
        headers = {
            # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0",
            "Referer":"",
            ":authority": "s3m3.nzwgs.com"
            ":path":""
        }
        r = requests.get(urlstr,headers=headers)

        soup = BeautifulSoup(r.text, 'html.parser')
        con = soup.find("ul",id="pins").find_all("li")
        for i in con:
            num = num + 1
            imgUrl = i.find("img").get('data-original')
            title = i.find("img").get('alt')
            save_txt(imgUrl,title)
            worksheet.write(num,0, label = '{}'.format(title))
            worksheet.write(num,1, label = '{}'.format(imgUrl))
            # obj = dict(title=title,imgUrl=imgUrl)
            # list.append(obj)
            download_biaoqingbaos(title,imgUrl)


def save_txt(*args):
    for i in args:
        with open('qiubai.txt', 'a', encoding='utf-8') as f:
            f.write(i+'\r\n')
      
# def download_biaoqingbaos(url, img_list):
#     response = requests.get(url)

#     for img in img_list:
#         image = img.get('data-original')
#         title = img.get('title')
#         print('下载图片： ', title)

#         try:
#             with open(path + title + os.path.splitext(image)[-1], 'wb') as f:
#                 img = requests.get(image).content
#                 f.write(img)
#         except OSError:
#             print('length  failed')
#             break
def download_biaoqingbaos(title,img_list):

    print('下载图片： ', title)
    with open('./test/' + title + os.path.splitext(img_list)[-1], 'wb') as f:
            img = requests.get(img_list).content
            f.write(img)

def main():
    # global num 
    # num = 1
    # for i in range(0,250,25):
    #     getHtml(25,num)
    t = threading.Thread(target=getHtml)
    t.setDaemon(True)
    t.start()
    t.join()
    c = threading.Thread(target=getHtml)
    c.setDaemon(True)
    c.start()
    c.join()
    print('{}正在下载'.format(threading.current_thread().name))
    # 保存
    workbook.save('Excel_test.xls')
if __name__ == '__main__':
    main()
